package net.goutalk.glcs.module.bi.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import net.goutalk.glcs.common.constant.GlobalConstant;
import net.goutalk.glcs.common.model.result.R;
import net.goutalk.glcs.common.page.ConventPage;
import net.goutalk.glcs.common.page.PageOutput;
import net.goutalk.glcs.common.utils.VoToColumnUtil;
import net.goutalk.glcs.module.bi.dto.*;
import net.goutalk.glcs.module.bi.entity.Visual;
import net.goutalk.glcs.module.bi.entity.VisualConfig;
import net.goutalk.glcs.module.bi.service.IVisualConfigService;
import net.goutalk.glcs.module.bi.service.IVisualService;
import net.goutalk.glcs.module.bi.vo.VisualPageVo;
import net.goutalk.glcs.module.bi.vo.VisualVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import net.goutalk.glcs.module.bi.dto.AddVisualDto;
import net.goutalk.glcs.module.bi.dto.UpdateVisualDto;
import net.goutalk.glcs.module.bi.dto.VisualCopyDto;
import net.goutalk.glcs.module.bi.dto.VisualPageDto;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * BI 大屏列表 前端控制器
 * </p>
 *
 * @author zjq
 * @since 2022-09-21
 */
@RestController
@RequestMapping(GlobalConstant.BI_MODULE_PREFIX)
@Api(value = GlobalConstant.BI_MODULE_PREFIX, tags = "BI大屏设计模块")
@AllArgsConstructor
public class VisualController {

    private final IVisualService visualService;

    private final IVisualConfigService visualConfigService;


    @GetMapping(value = "/page")
    @ApiOperation(value = "根据分类配置获取列表数据/(分页)")
    public R page(VisualPageDto dto) {
        LambdaQueryWrapper<Visual> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(dto.getKeyword()), Visual::getTitle, dto.getKeyword())
                .eq(StrUtil.isNotBlank(dto.getCategory().toString()), Visual::getCategory, dto.getCategory())
                .select(Visual.class, x -> VoToColumnUtil.fieldsToColumns(VisualPageVo.class).contains(x.getProperty()));

        IPage<Visual> page = visualService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<VisualPageVo> pageOutput = ConventPage.getPageOutput(page, VisualPageVo.class);

        return R.ok(pageOutput);
    }


    @GetMapping(value = "/info")
    @ApiOperation("根据id查询模板详情")
    public R info(@Valid @PathParam("id") Long id) {
        VisualVo visualvo = BeanUtil.toBean(visualService.getById(id), VisualVo.class);
        VisualConfig visualCongfig = visualConfigService.getByVisualId(id);
        JSONObject result = new JSONObject();
        result.put("config", visualCongfig);
        result.put("visual", visualvo);
        return R.ok(result);
    }

    @PostMapping
    @ApiOperation(value = "新增Visaul")
    public R add(@Valid @RequestBody AddVisualDto dto) {
        Map<String, Object> result = visualService.add(dto);
        return R.ok(result);
    }


    @PutMapping
    @ApiOperation(value = "更新分类")
    public R update(@Valid @RequestBody UpdateVisualDto dto) {
        Visual visual = BeanUtil.toBean(dto.getVisual(), Visual.class);
        VisualConfig visualConfig = BeanUtil.toBean(dto.getConfig(), VisualConfig.class);
        return R.ok(visualService.updateVisual(dto.getId(), visual, visualConfig));
    }

    @PostMapping("/copy")
    @ApiOperation(value = "拷贝")
    public R copy(@Valid @RequestBody VisualCopyDto dto) {
        Long newVisualId = visualService.copy(dto);
        return R.ok(newVisualId);
    }


    @DeleteMapping
    @ApiOperation(value = "删除")
    public R delete(@RequestBody List<Long> ids) {
        return R.ok(visualService.removeBatchByIds(ids));
    }

}
